@echo off
setlocal enabledelayedexpansion

:: Windows Property Cleaning System - Clean Installation Script
:: This script uses only ASCII characters to avoid encoding issues

echo ================================================
echo Property Cleaning System - Windows Installation
echo ================================================
echo.

:: Check for admin rights
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: Administrator rights required!
    echo Please right-click and select "Run as administrator"
    pause
    exit /b 1
)

echo [1/5] Checking system compatibility...
ver | find "Windows 10" >nul
if %errorlevel% neq 0 (
    ver | find "Windows 11" >nul
    if %errorlevel% neq 0 (
        echo WARNING: This script is optimized for Windows 10/11
    )
)

:: Check 64-bit system
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x64" >nul
if %errorlevel% neq 0 (
    echo ERROR: 64-bit Windows required!
    pause
    exit /b 1
)

echo [2/5] Installing Chocolatey...
if not exist "%PROGRAMDATA%\chocolatey\bin\choco.exe" (
    powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
    if %errorlevel% neq 0 (
        echo ERROR: Chocolatey installation failed!
        echo Please check internet connection and try again
        pause
        exit /b 1
    )
) else (
    echo Chocolatey already installed
)

:: Refresh PATH
set "PATH=%PATH%;%PROGRAMDATA%\chocolatey\bin"

echo [3/5] Installing required software...

:: Install Git
where git >nul 2>&1
if %errorlevel% neq 0 (
    choco install git -y --no-progress
) else (
    echo Git already installed
)

:: Install Docker Desktop
where docker >nul 2>&1
if %errorlevel% neq 0 (
    choco install docker-desktop -y --no-progress
) else (
    echo Docker already installed
)

:: Install OpenJDK 17
where java >nul 2>&1
java -version 2>&1 | find "17" >nul
if %errorlevel% neq 0 (
    choco install openjdk17 -y --no-progress
) else (
    echo Java 17 already installed
)

:: Install Node.js
where node >nul 2>&1
if %errorlevel% neq 0 (
    choco install nodejs-lts --version=18.18.2 -y --no-progress
) else (
    echo Node.js already installed
)

echo [4/5] Creating project directories...
set "PROJECT_DIR=%USERPROFILE%\property-cleaning-system"
if not exist "%PROJECT_DIR%" (
    mkdir "%PROJECT_DIR%"
)

:: Create startup scripts
echo [5/5] Creating startup scripts...

:: Quick start script
echo @echo off > "%PROJECT_DIR%\start-system.bat"
echo echo Starting Property Cleaning System... >> "%PROJECT_DIR%\start-system.bat"
echo cd /d "%~dp0" >> "%PROJECT_DIR%\start-system.bat"
echo docker-compose up -d >> "%PROJECT_DIR%\start-system.bat"
echo timeout /t 10 /nobreak ^>nul >> "%PROJECT_DIR%\start-system.bat"
echo start http://localhost:3000 >> "%PROJECT_DIR%\start-system.bat"
echo start http://localhost:8080/swagger-ui.html >> "%PROJECT_DIR%\start-system.bat"
echo echo System started! >> "%PROJECT_DIR%\start-system.bat"
echo pause >> "%PROJECT_DIR%\start-system.bat"

:: Environment check script
echo @echo off > "%PROJECT_DIR%\check-env.bat"
echo echo Checking system environment... >> "%PROJECT_DIR%\check-env.bat"
echo where git ^>nul 2^>^&1 >> "%PROJECT_DIR%\check-env.bat"
echo if %%errorlevel%% equ 0 echo Git: OK >> "%PROJECT_DIR%\check-env.bat"
echo where docker ^>nul 2^>^&1 >> "%PROJECT_DIR%\check-env.bat"
echo if %%errorlevel%% equ 0 echo Docker: OK >> "%PROJECT_DIR%\check-env.bat"
echo where java ^>nul 2^>^&1 >> "%PROJECT_DIR%\check-env.bat"
echo if %%errorlevel%% equ 0 echo Java: OK >> "%PROJECT_DIR%\check-env.bat"
echo where node ^>nul 2^>^&1 >> "%PROJECT_DIR%\check-env.bat"
echo if %%errorlevel%% equ 0 echo Node.js: OK >> "%PROJECT_DIR%\check-env.bat"
echo pause >> "%PROJECT_DIR%\check-env.bat"

:: Create desktop shortcuts
echo Set oWS = WScript.CreateObject("WScript.Shell") > "%PROJECT_DIR%\create-shortcuts.vbs"
echo sLinkFile = oWS.SpecialFolders("Desktop") ^& "\Property Cleaning System.lnk" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.TargetPath = "%PROJECT_DIR%\start-system.bat" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.WorkingDirectory = "%PROJECT_DIR%" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.Save >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = Nothing >> "%PROJECT_DIR%\create-shortcuts.vbs"

"%PROJECT_DIR%\create-shortcuts.vbs"
del "%PROJECT_DIR%\create-shortcuts.vbs"

echo.
echo ================================================
echo Installation Complete!
echo ================================================
echo.
echo Project directory: %PROJECT_DIR%
echo Desktop shortcut: Property Cleaning System
echo.
echo Next steps:
echo 1. Restart your computer (recommended)
echo 2. Run the desktop shortcut or start-system.bat
echo 3. Access the system at http://localhost:3000
echo.
echo For troubleshooting, run: %PROJECT_DIR%\check-env.bat
pause